from lxml import etree

root = etree.Element("Links")

link1 = etree.SubElement(root, "link1")
link1.append( etree.Element("PARENT", name="link0") )
link1.append( etree.Element("DH", a="1", d="2", alpha="3", theta="4") )
link1.append( etree.Element("GRAPHICS", parameters="0,0,0,0,0") )

link2 = etree.SubElement(root, "link2")
link2.append( etree.Element("PARENT", name="link0") )
link2.append( etree.Element("DH", a="1", d="2", alpha="3", theta="5") )
link2.append( etree.Element("GRAPHICS", parameters="0,0,0,0,0") )

link3 = etree.SubElement(root, "link3")
link3.append( etree.Element("PARENT", name="link0") )
link3.append( etree.Element("DH", a="1", d="2", alpha="3", theta="4") )
link3.append( etree.Element("GRAPHICS", parameters="0,0,0,0,0") )

link4 = etree.SubElement(root, "link4")
link4.append( etree.Element("PARENT", name="link0") )
link4.append( etree.Element("DH", a="1", d="2", alpha="3", theta="4") )
link4.append( etree.Element("GRAPHICS", parameters="0,0,0,0,0") )

string = etree.tostring(root, pretty_print=True)
print string

f = open('links.xml', 'w')
f.write(string)
f.close()
